package com.guoqing.example.acvice;

import com.guoqing.example.exception.LyException;
import com.guoqing.example.vo.ExceptionResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CommonExceptionHandler {

    // solution 1
    /*@ExceptionHandler(LyException.class)
    public ResponseEntity<String> handlerException(LyException e){
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }*/

    // solution 2
    /*@ExceptionHandler(LyException.class)
    public ResponseEntity<String> handlerException(LyException e){
        ExceptionEnum exceptionEnum = e.getExceptionEnum();
        return ResponseEntity.status(exceptionEnum.getCode()).body(exceptionEnum.getMsg());
    }*/

    // solution 3
    @ExceptionHandler(LyException.class)
    public ResponseEntity<ExceptionResult> handlerException(LyException e) {
        return ResponseEntity.status(e.getExceptionEnum().getCode())
                .body(new ExceptionResult(e.getExceptionEnum()));
    }

}
